package com.shb.jutils.sql;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SqlBuilder {
	private String paramName;
	private ParamNameConverter converter;
	private boolean ignoreNullorEmpty = true;
	private StringBuilder sqlExceptSelect = new StringBuilder();
	private String select;
	private List<String> paramList = new ArrayList<String>();

	public SqlBuilder() {
		super();
	}

	public SqlBuilder(String select) {
		this.select = select;
	}
	
	public SqlBuilder(String select, String sqlExceptSelect) {
		this.select = select;
		this.sqlExceptSelect.append(sqlExceptSelect);
	}

	/**
	 * 参数注入方式拼接sql，将要注入的参数保存到paramList
	 * @param _paramName
	 * @param param
	 * @param condType
	 * @param _ignoreNullorEmpty
	 */
	public void addParam(String _paramName, Object param,
			ConditionType condType, boolean _ignoreNullorEmpty) {
		
		if(_paramName == null || "".equals(_paramName.trim())){
			throw new IllegalArgumentException("数据库字段名参数不能为空");
		}
		
		if(_ignoreNullorEmpty && (param == null || "".equals(param.toString().trim()))){
			return ;
		}
		
		//paramName自定义转换
		if (converter != null){
			paramName = converter.convert(_paramName);
		}else {
			paramName = _paramName;
		}
		
		switch (condType) {
		case EQUALS:
			sqlExceptSelect.append(" and " + paramName + "=?");
			paramList.add(String.valueOf(param));
			break;
		case NOT_EQUALS:
			sqlExceptSelect.append(" and " + paramName + " <> ?");
			paramList.add(String.valueOf(param));
			break;
		case LIKE:
			sqlExceptSelect.append(" and " + paramName + " like ?");
			paramList.add("%" + String.valueOf(param) + "%");
			break;
		case LEFT_LIKE:
			sqlExceptSelect.append(" and " + paramName + " like ?");
			paramList.add("%" + String.valueOf(param));
			break;
		case RIGHT_LIKE:
			sqlExceptSelect.append(" and " + paramName + " like ?");
			paramList.add(String.valueOf(param) + "%");
			break;
		case OVER:
			sqlExceptSelect.append(" and " + paramName + " > ?");
			paramList.add(String.valueOf(param));
			break;
		case LESS:
			sqlExceptSelect.append(" and " + paramName + " < ?");
			paramList.add(String.valueOf(param));
			break;
		case OVER_OR_EQUALS:
			sqlExceptSelect.append(" and " + paramName + " >= ?");
			paramList.add(String.valueOf(param));
			break;
		case LESS_OR_EQUALS:
			sqlExceptSelect.append(" and " + paramName + " <= ?");
			paramList.add(String.valueOf(param));
			break;
		default:
			break;
		}
	}

	/**
	 * 使用默认配置：
	 * 是否忽略空白参数：true
	 * @param _paramName
	 * @param param
	 * @param condType
	 */
	public void addParam(String _paramName, Object param, ConditionType condType){
		addParam(_paramName, param, condType, ignoreNullorEmpty);
	}
	
	/**
	 * 拼接sql语句，将要注入的参数保存到paramList
	 * @param appendSql
	 * @param param
	 * @param _ignoreNullorEmpty
	 */
	public void addParam(String appendSql, Object param, boolean _ignoreNullorEmpty){
		if(_ignoreNullorEmpty){
			if(param == null || "".equals(param.toString().trim())){
				return ;
			}
		}
		
		sqlExceptSelect.append(appendSql);
		paramList.add(String.valueOf(param));
	}
	
	public void addParam(String appendSql, Object param){
		if(ignoreNullorEmpty && (param == null || "".equals(param.toString().trim()))){
			return ;
		}
		
		sqlExceptSelect.append(appendSql);
		paramList.add(String.valueOf(param));
	}
	
	public void addParam(String appendSql, Object... params){
		List<String> tempParam = new ArrayList<String>();
		for(Object ob : params){
			if(ignoreNullorEmpty && (ob == null || "".equals(ob.toString().trim()))){
				return ;
			}else {
				tempParam.add(String.valueOf(ob));
			}
		}
		sqlExceptSelect.append(appendSql);
		paramList.addAll(tempParam);
	}
	
	/**
	 * 直接拼接sql
	 * @param appendSql
	 */
	public void appendSql(String appendSql){
		sqlExceptSelect.append(appendSql);
	}
	
	/**
	 * 根据Map集合拼接sql
	 * @param paramMap
	 * @param defaultCondType
	 * @param _ignoreNullorEmpty
	 */
	public void addParam(Map<String, Object> paramMap,ConditionType defaultCondType, boolean _ignoreNullorEmpty){
		for(Map.Entry<String, Object> entry : paramMap.entrySet()){
			Object param = entry.getValue();
			Object paramValue = null;
			if(param.getClass().isArray()){
				paramValue = ((Object[]) param)[0] ;
			}else {
				paramValue = param;
			}
			
			addParam(entry.getKey(), paramValue, defaultCondType, _ignoreNullorEmpty);
		}
	}
	
	/**
	 * 获取生成的sql字符串
	 * 
	 * @return
	 */
	public String getBuildSql() {
		return select + this.sqlExceptSelect.toString();
	}

	public StringBuilder getSqlExceptSelect() {
		return sqlExceptSelect;
	}

	public void setSqlExceptSelect(StringBuilder sqlExceptSelect) {
		this.sqlExceptSelect = sqlExceptSelect;
	}

	public String getSelect() {
		return select;
	}

	public void setSelect(String select) {
		this.select = select;
	}

	public ParamNameConverter getConverter() {
		return converter;
	}

	public void setConverter(ParamNameConverter converter) {
		this.converter = converter;
	}

	public boolean isIgnoreNullorEmpty() {
		return ignoreNullorEmpty;
	}

	public void setIgnoreNullorEmpty(boolean ignoreNullorEmpty) {
		this.ignoreNullorEmpty = ignoreNullorEmpty;
	}

	public List<String> getParamList() {
		return paramList;
	}
}
